package com.sinodata.bsm.cicp.finder.hp;

import java.io.BufferedReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;

import com.sinodata.bsm.cicp.util.CommandReader;

/**
 * HP-UX平台网络接口发现程序
 */
public class Net extends HPFinder {
    private static final Logger logger = Logger.getLogger(Net.class);
    private static final String SHELL = "netstat -in|grep -v Name";

    public static void main(String[] args) {

    }

    @SuppressWarnings("unchecked")
    @Override
    public List<String[]> find(String ip, Map params) {
        List<String[]> list = new ArrayList<String[]>();
        String[] netRes = null;
        BufferedReader reader = null;
        String strLine = null;
        try {
            reader = CommandReader.getReader(ip, SHELL, 0, 0);
            while ((strLine = reader.readLine()) != null) {
                if (strLine.length() > 0) {
                    String name = strLine.substring(0, strLine.indexOf(" ")).trim();
                    if (name.endsWith("*")) {
                        continue;
                    }
                    netRes = new String[4];
                    list.add(netRes);
                    netRes[0] = name;
                    netRes[1] = name;
                    netRes[2] = getHostName(ip) + " " + name;
                    netRes[3] = name; //物理接口名
                }
            }
            return list;
        } catch (Exception e) {
            logger.error("failed to find mem size", e);
        } finally {
            if (reader != null) {
                try {
                    reader.close();
                } catch (IOException e) {
                    logger.error("failed to close reader", e);
                }
            }
        }
        return null;
    }
}
